package Application.Components.Schedule;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.LinkedList;

public class ScheduleDataManager {
	public static final String DATE_FORMAT		=	"YYYYMMdd";
	public static final String SQLServerDriver	= "com.microsoft.sqlserver.jdbc.SQLServerDriver";
	
	public static final String SQL_SERVER		= "jdbc:sqlserver://bakalari.ssinfis.local:1529;";
	
	public static final String SQL_USERNAME		= "sporic";
	public static final String SQL_PASSWORD		= "sporic";
	
	private Driver driver;
	private Connection connection;
	private DatabaseMetaData databaseMetaData;
	
	private Statement statement;
	private ResultSet resultSet;
	
	private SimpleDateFormat dateFormat;
	
	private String roomNumber;
	
	private String[] mon;
	private String[] tue;
	private String[] wed;
	private String[] thu;
	private String[] fri;
	
	public ScheduleDataManager(int roomNumber){
		this.roomNumber = "" + roomNumber;
	}
	
	public void connect(){
		try {
			this.driver				= (Driver)Class.forName(SQLServerDriver).newInstance();
			this.connection			= DriverManager.getConnection(SQL_SERVER, SQL_USERNAME, SQL_PASSWORD);
			this.databaseMetaData	= connection.getMetaData();
			this.statement			= connection.createStatement();	
			this.dateFormat 		= new SimpleDateFormat(DATE_FORMAT);
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
	
	public String[] getMonday(){
		return mon;
	}
	
	public String[] getTuesday(){
			return tue;
	}

	public String[] getWednesday(){
		return wed;
	}

	public String[] getThursday(){
		return thu;
	}

	public String[] getFriday(){
		return fri;
	}
	
	public void updateScheduleData(){
		
		/*Calendar c = Calendar.getInstance();
		c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
		ArrayList<RozvrhovaAkce> pondeli = getRozvrhUcebny(c.getTime(), ucebna);
		c.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY);
		ArrayList<RozvrhovaAkce> utery = getRozvrhUcebny(c.getTime(), ucebna);
		c.set(Calendar.DAY_OF_WEEK, Calendar.WEDNESDAY);
		ArrayList<RozvrhovaAkce> streda = getRozvrhUcebny(c.getTime(), ucebna);
		c.set(Calendar.DAY_OF_WEEK, Calendar.THURSDAY);
		ArrayList<RozvrhovaAkce> ctvrtek = getRozvrhUcebny(c.getTime(), ucebna);
		c.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);
		ArrayList<RozvrhovaAkce> patek = getRozvrhUcebny(c.getTime(), ucebna);*/
		Calendar c = Calendar.getInstance();
		
		c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
		mon = getSingleDayData(c.getTime());
		
		c.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY);
		tue = getSingleDayData(c.getTime());
		
		c.set(Calendar.DAY_OF_WEEK, Calendar.WEDNESDAY);
		wed = getSingleDayData(c.getTime());
		
		c.set(Calendar.DAY_OF_WEEK, Calendar.THURSDAY);
		thu = getSingleDayData(c.getTime());
		
		c.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);
		fri = getSingleDayData(c.getTime());
	}
	
	public String[] getSingleDayData(Date date){
		try {
			LinkedList<String> tmpLessons	=	new LinkedList<>();
			String day = dateFormat.format(date);
			
				resultSet = statement.executeQuery(
							"SELECT "+
									"(bakalari.dbo.s_rozvrh.HOD-3) as hodina, " +
									"bakalari.dbo.[nazvy_pr].ZKRATKA as predmet, " +
									"mistnost.ZKRATKA as mistnost, " +
									"tridy.ZKRATKA as trida, "+
									"bakalari.dbo.ucitele.ZKRATKA as ucitel "+
							"FROM " +
									"bakalari.dbo.[s_rozvrh], bakalari.dbo.[nazvy_pr], bakalari.dbo.mistnost, bakalari.dbo.tridy, bakalari.dbo.ucitele "+ 
							"WHERE "+ 
									"den = '"+ day +"' and "+ 
									"s_rozvrh.KOD_TRID = tridy.KOD_TRID and "+
									"s_rozvrh.KOD_UCIT = ucitele.INTERN_KOD and "+
									"s_rozvrh.KOD_MIST  = mistnost.KOD_MIST and "+ 
									"s_rozvrh.KOD_PRED = bakalari.dbo.nazvy_pr.KOD_PRED and "+
									"mistnost.ZKRATKA = '"+ roomNumber +"' "+
							"ORDER BY HOD;");
			
			while(resultSet.next()){
				String lessonNumber		=	resultSet.getString("hodina").trim();
				String lessonName		=	resultSet.getString("predmet").trim();
				String className		=	resultSet.getString("trida").trim();
				String teacher			=	resultSet.getString("ucitel").trim();
				
				String lesson = lessonNumber + "," + lessonName + "," + className + "," + teacher;
				
				if(tmpLessons.isEmpty()){
					tmpLessons.add(lesson);
				}
				else{
					String[] tmpLessonsSplit	= tmpLessons.getLast().split(",");
					String[] lessonSplit		= lesson.split(",");
					
					
					if(tmpLessonsSplit[0].equals(lessonSplit[0])){	//Stejne cislo hodiny
						if(!tmpLessonsSplit[2].equals(lessonSplit[2])){ //nerovna se trida
							tmpLessonsSplit[2] = tmpLessonsSplit[2] + " " + lessonSplit[2];
							
							String newCorrectLesson = tmpLessonsSplit[0] + "," + tmpLessonsSplit[1] + "," + tmpLessonsSplit[2] + "," + tmpLessonsSplit[3];
							
							tmpLessons.removeLast();
							tmpLessons.add(newCorrectLesson);
						}
						//Stejna hodina i tridy (pravdepodobne stejnej zaznam)
					}
					else tmpLessons.add(lesson);
				}
			}
			
			String[] tmpArray = new String[tmpLessons.size()];
			
			for(int i = 0; i < tmpArray.length; i++){
				tmpArray[i] = tmpLessons.get(i);
			}
			return tmpArray;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		System.out.println("[i] ScheduleDataManager is returning null!");
		return null;
	}
	
	public void cleanUp(){
		try {
			this.connection.close();
			System.out.println("[i] Connection to Database has been closed!");
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}
